//
//  MHHomeMainViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/24.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHHomeMainViewController.h"
#import "MHHomeMainHeaderView.h"
#import "MHComicDetailViewController.h"
#import "MHComicCollectionViewCell.h"




@interface MHHomeMainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
//@property (nonatomic, strong) MHHomeMainHeaderView * headerView;

@end

@implementation MHHomeMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    
    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//    label.text = @"Home";
//    label.font = [UIFont systemFontOfSize:20];
//    [self.view addSubview:label];

    
    
  
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
       self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT  - 40)collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    // items的宽和高
    float width = (SCREEN_WIDTH - 60) / 3;
    float height = width * 1.5;
    layout.itemSize = CGSizeMake(width, height);
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    // minimumLineSpacing: 设置行间距
    layout.minimumLineSpacing = 15;
    // minimumInteritem：设置item间距。
    layout.minimumInteritemSpacing = 15;
    

    
    //注册cell
    [_collectionView registerClass:[MHComicCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    // 注册表头的View
    [_collectionView registerClass:[MHHomeMainHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    layout.headerReferenceSize = CGSizeMake(0, 200);
    
    
    

    
    
    [self.view addSubview:_collectionView];
}


// 点击事件，跳转到详情界面
- (void)didGotoDetailAction:(UIButton *)button
{
    MHComicDetailViewController *comicDetailVC = [[MHComicDetailViewController alloc]init];
    [self.navigationController pushViewController:comicDetailVC animated:YES];
}

#pragma mark - DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 22;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicDetailViewController *comicDetailVC = [[MHComicDetailViewController alloc]init];
    [self.navigationController pushViewController:comicDetailVC animated:YES];
}

// 表头的代理
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
   
        MHHomeMainHeaderView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
     
        reusableView.backgroundColor = [UIColor blueColor];
    
        // 为_headerView的scrollView的6个button设置点击事件
        for (int i = 0; i < 6; i++) {
            [((UIButton *)reusableView.scrollView.subviews[i]) addTarget:self action:@selector(didGotoDetailAction:) forControlEvents:UIControlEventTouchUpInside];
        }
    
    
        return reusableView;
    
    
}




@end
